package cn.framelife.rainbow.ui.wizard;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;

import cn.framelife.rainbow.constant.Constant;

import cn.framelife.rainbow.db.creator.ICodeCreator;
import cn.framelife.rainbow.db.mysql.MySQLCodeCreator;
import cn.framelife.rainbow.db.oracle.OracleCodeCreator;
import cn.framelife.rainbow.ui.entity.ReverseProperty;
import cn.framelife.rainbow.ui.entity.WizardMessage;
import cn.framelife.rainbow.ui.view.ErrorMessageBox;
import cn.framelife.rainbow.util.DatabaseCache;
import cn.framelife.rainbow.util.ProjectUtil;


/**
 * 反向功能向导
 * 
 * @ClassName: ReverseWizard
 * @author 潘广伟 p_3er@qq.com
 * @date 2013年10月31日 下午6:28:11
 * 
 */
public class ReverseWizard extends Wizard implements INewWizard {
	
	private ConnectionPage connectionPage;
	private ModulePage modulePage;
	private ReversePage  secondPage;
	private ReverseProperty reverseProperty;
	
	private ICodeCreator creator;

	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.reverseProperty = new ReverseProperty();
	}

	/**
	 * Finish按钮事件
	 */
	@Override
	public boolean performFinish() {
		if (modulePage.isComfirm()) {
			
			String driverClass = DatabaseCache.getConnectionProperty().getDriverClass();

			try {
				/**
				 * 这里可以换成不同的数据库
				 */
				if(driverClass.equals(Constant.MYSQLDRIVER)){
					creator = new MySQLCodeCreator();
				}else if(driverClass.equals(Constant.ORACLEDRIVER)){
					creator = new OracleCodeCreator();
				}
		
				new ProgressMonitorDialog(PlatformUI.getWorkbench().getDisplay().getActiveShell()).run(false, false, new IRunnableWithProgress() {
                    public void run(IProgressMonitor monitor){
                        monitor.beginTask("JRainbow task", 100);
                        monitor.setTaskName("JRainbow runing");
                        try {
                        	monitor.worked(2);
                        	creator.init(reverseProperty);
                        	monitor.worked(5);
                        	creator.createCode(modulePage.getModuleData());
                        	monitor.worked(85);
                        	ProjectUtil.refreshProject(reverseProperty.getSelectionProjectName());
                        	monitor.worked(100);
						} catch (Exception e) {
							e.printStackTrace();
							ErrorMessageBox.showBox(getShell(), e.toString());
						}
                        
                        monitor.done();
                    } 
                });
				
				
				
			} catch (Exception e) {
				ErrorMessageBox.showBox(getShell(), e.toString());
				e.printStackTrace();
				return false;
			}
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 增加向导的内容显示
	 */
	public void addPages() {
		setWindowTitle("JRainbow Reverse Engineering");

		WizardMessage message = new WizardMessage();
		
		message.setPageTitle("Database Connection Driver");
		message.setPageDescription("Create a new connection driver");
		connectionPage = new ConnectionPage(message);
		
		message.setPageTitle("Multi Module Generation");
		message.setPageDescription("Select tables for module");
		modulePage = new ModulePage(message,reverseProperty);
		
		message.setPageTitle("JRainbow Application Generation");
		message.setPageDescription("Generate Java classes and web page from tables");
		secondPage = new ReversePage(message,reverseProperty);
		
		addPage(connectionPage);
		addPage(modulePage);
		addPage(secondPage);
	}
}
